package Demo01;

import java.util.Stack;

class MyQueue {
    private Stack<Integer> stack1;
    private Stack<Integer> stack2;
    public MyQueue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    public void push(int x) {
        if(!stack1.isEmpty()) {
            stack1.push(x);
        } else if (!stack2.isEmpty()) {
            stack2.push(x);
        } else {
            stack1.push(x);
        }
    }

    public int pop() {
        if(!stack1.isEmpty()) {
            int ret = -1;
            int size = stack1.size();
            for (int i = 0; i < size-1; i++) {
                ret = stack1.pop();
                stack2.push(ret);
            }
            ret = stack1.pop();
            int tmp = -1;
            int size2 = stack2.size();
            for (int i = 0; i < size2; i++) {
                tmp = stack2.pop();
                stack1.push(tmp);
            }
            return ret;
        } else {
            return -1;
        }
    }

    public static void main(String[] args) {
        MyQueue queue = new MyQueue();
        queue.push(1);
        queue.push(2);
        System.out.println(queue.peek());
        System.out.println(queue.pop());
    }
    public int peek() {
        if(!stack1.isEmpty()) {
            int ret = -1;
            int size = stack1.size();
            for (int i = 0; i < size; i++) {
                ret = stack1.pop();
                stack2.push(ret);
            }
            ret = stack2.peek();
            int tmp = -1;
            int size2 = stack2.size();
            for (int i = 0; i < size2; i++) {
                tmp = stack2.pop();
                stack1.push(tmp);
            }
            return ret;
        }  else {
            return -1;
        }
    }

    public boolean empty() {
        return stack2.isEmpty()&&stack1.isEmpty();
    }
}